

public class FitnessCalc {

    static byte[] solution = new byte[64];

    /* Public methods */
    // Set a candidate solution as a byte array
    public static void setSolution(byte[] newSolution) {
        solution = newSolution;
    }

    // To make it easier we can use this method to set our candidate solution 
    // with string of 0s and 1s
    static void setSolution(String newSolution) {
        solution = new byte[newSolution.length()];
        // Loop through each character of our string and save it in our byte 
        // array
        for (int i = 0; i < newSolution.length(); i++) {
            String character = newSolution.substring(i, i + 1);
            if (character.contains("0") || character.contains("1")) {
                solution[i] = Byte.parseByte(character);
            } else {
                solution[i] = 0;
            }
        }
    }

    // Calculate individuals fitness by comparing it to our candidate solution
    static double getFitness(Individual individual) {
        double fitness;
        double x = individual.getGene(0);
        double y = individual.getGene(1);
        // Loop through our individuals genes and compare them to our candidates
        
        fitness = (3*Math.pow((1-x),2) * Math.pow(Math.E,(-(Math.pow(x,2)))-Math.pow((y+1),2))) - (10 * (x/5 - Math.pow(x, 3) - Math.pow(y,5)) * Math.pow(Math.E,(-(Math.pow(x,2)-Math.pow(y,2))))) - (1/3 * Math.pow(Math.E,(-Math.pow((x+1),2)) - Math.pow(y,2)));
        return fitness;
    }
    
    // Get optimum fitness
    static int getMaxFitness() {
        int maxFitness = solution.length;
        return maxFitness;
    }
}
